package indi.yangshenhui.tool;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class StringUtil {
	public static final String BASE = "abcdefghijklmnopqrstuvwxyz0123456789";

	// 屏蔽构造方法
	private StringUtil() {
	}

	/**
	 * 返回两位小数的标准价格字符串 getFormatPrice
	 * 
	 * @param price
	 * @return String
	 * @exception @since
	 *                1.0.0
	 */
	public static String getFormatPrice(BigDecimal price) {
		if (price == null) {
			return "";
		} else {
			return price.setScale(2, BigDecimal.ROUND_HALF_UP) + "";
		}
	}

	/**
	 * 根据正则表达式过滤字符串 filterStr
	 * 
	 * @param str
	 * @param matcher
	 * @return String
	 * @exception @since
	 *                1.0.0
	 */
	public static String filterStr(String str, String matcher) {
		if (StringUtils.isNotBlank(str)) {
			Pattern p = Pattern.compile(matcher);
			Matcher m = p.matcher(str);
			return m.replaceAll("").trim();
		} else {
			return "";
		}
	}

	/**
     * <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
     *
     * <pre>
     * StringUtils.isBlank(null)      = true
     * StringUtils.isBlank("")        = true
     * StringUtils.isBlank(" ")       = true
     * StringUtils.isBlank("bob")     = false
     * StringUtils.isBlank("  bob  ") = false
     * </pre>
     */
	public static boolean isEmpty(String s) {
		if (s == null) {
			return true;
		}
		return StringUtils.isBlank(s);
	}

	public static boolean isNotEmpty(String s) {
		return !isEmpty(s);
	}

	/**
	 * 获得一个随机的字符串
	 * 
	 * @param length
	 *            字符串的长度
	 * @return 随机字符串
	 */
	public static String getRandomString(int length) {
		Random random = new Random();
		StringBuffer buffer = new StringBuffer();
		if (length < 1) {
			length = 1;
		}
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(BASE.length());
			buffer.append(BASE.charAt(number));
		}
		return buffer.toString();
	}

	/**
	 * 首字母转换为大写
	 * 
	 * @param s
	 * @return String
	 */
	public static String initialsCapital(String s) {
		if (isEmpty(s)) {
			return null;
		}
		int length = s.length();
		char c = s.charAt(0);
		if (Character.isUpperCase(c)) {
			return s;
		}
		return Character.toUpperCase(c) + s.substring(1, length);
	}

	/**
	 * 格式化金额 parsePrice
	 * 
	 * @param s
	 * @param len
	 * @return String
	 * @exception @since
	 *                1.0.0
	 */
	public static String parsePrice(String s, int len) {
		if (StringUtils.isBlank(s) || Double.parseDouble(s) == 0) {
			return "0.00";
		}
		NumberFormat format = null;
		double num = Double.parseDouble(s);
		if (len == 0) {
			format = new DecimalFormat("#################.00");

		} else {
			StringBuffer buff = new StringBuffer();
			buff.append("################0.");
			for (int i = 0; i < len; i++) {
				buff.append("0");
			}
			format = new DecimalFormat(buff.toString());
		}
		String result = format.format(num);
		return result;
	}

	/**
	 * 格式化数字:51,234.00 parsePriceWithComma
	 * 
	 * @param price
	 * @return String
	 * @exception @since
	 *                1.0.0
	 */
	public static String parsePrice(double price) {
		DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
		return decimalFormat.format(price);
	}

	public static String parsePrice(String s) {
		return parsePrice(s, 2);
	}

}